package com.mco.config.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

/**
 * redis 配置
 *
 * @author admin
 */
@Component
public class RedisConfig {
    /**
     * redis序列化的工具配置类下面这个请一定开启配置
     * 不127.0.0.1:6379> keys *
     * 1)“ord:102” 序列化过
     * 野生，没有序列化过*2)“\xaclxe\x00\x05tlxeelaord:102”
     * this.redisTemplate.opsForValue();
     * //提供了操作string类型的所有方法
     * this,redisTemplate.opsForList山; // 供了换ist类型的所有方法
     * this.redisTemplate.opsForset(); //挺供了换作set的所有方法
     * * this.redisTemplate.opsForHash(); //提供了操作hash表的所有方法
     * *this.redisTemplate.opsForZSet(); //提供了换作zset的所有方法
     *
     * @param lettuceConnectionFactory
     * @return
     */

    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
